package com.thtn.greendroidthtneg2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.util.Log;

public class DecompressZip {
	private static final String TAG = "DecompressZip";
	private static final int BUFFER_SIZE = 8192;
	private byte[] _buffer;
	private String _location;
	private String _zipFile;

	public DecompressZip(String paramString1, String paramString2) {
		this._zipFile = paramString1;
		this._location = paramString2;
		this._buffer = new byte[8192];
		dirChecker("");
	}

	private void dirChecker(String paramString) {
		File localFile = new File(this._location + paramString);
		if (!localFile.isDirectory())
			localFile.mkdirs();
	}

	public void unzip() {
		try {
			File f = new File(_location);
			if (!f.isDirectory()) {
				f.mkdirs();
			}
			ZipInputStream zin = new ZipInputStream(new FileInputStream(
					_zipFile));
			try {
				ZipEntry ze = null;
				while ((ze = zin.getNextEntry()) != null) {
					String path = _location + ze.getName();

					if (ze.isDirectory()) {
						File unzipFile = new File(path);
						if (!unzipFile.isDirectory()) {
							unzipFile.mkdirs();
						}
					} else {
						FileOutputStream fout = new FileOutputStream(path,
								false);
						try {
							for (int c = zin.read(); c != -1; c = zin.read()) {
								fout.write(c);
							}
							zin.closeEntry();
						} finally {
							fout.close();
						}
					}
				}
			} finally {
				zin.close();
			}
		} catch (Exception e) {
			Log.e(TAG, "Unzip exception", e);
		}
	}

}
